package com.roc.web.action.windcontroll;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.bean.SwitchBean;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblInsInf;
import com.roc.service.ISwitchsService;
import com.roc.service.IorgService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
@SuppressWarnings("serial")
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/switchs")
public class SwitchAction extends PaginationBaseAction {
	private Logger logger=LogManager.getLogger(SwitchAction.class);
	private SwitchSearchCondition searchCondition;
	private SwitchBean bean;
	private List<TblInsInf> orgList=new ArrayList<TblInsInf>(); 
	@Autowired
	IPageService pagenitionService;
	@Autowired
	IorgService orgService;
	@Autowired
	ISwitchsService switchService;
	protected void org()throws Exception{
		orgList=orgService.queryOrgList();
	}
	/**
	 * 初始化交易控制页面(商户)
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/windControllSta/switchList.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String init() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			logger.info("D0交易控制页面初始化");
			org();
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;	   
    }
	
	/**
	 * 交易控制列表页面(商户)
	 * @return
	 * @throws Exception
	 */
	@Action(value = "switchMcht", results = { @Result(name = "success", location = "/pages/windControllSta/switchList.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String blackView() throws Exception{
		try {
			if(searchCondition==null){
				searchCondition=(SwitchSearchCondition)getSearchConditionFromSession();
				if(searchCondition==null){
					searchCondition=new SwitchSearchCondition();
				}
			}		
			org();
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);				
				List<SwitchBean> lists=new ArrayList<SwitchBean>();
				if(searchRsList!=null&&searchRsList.size()>0)
				{
					Iterator<?> iterator=searchRsList.iterator();
					while(iterator.hasNext())
					{
						Object[] obj=(Object[]) iterator.next();
						SwitchBean bean=new SwitchBean();
						bean.setInsCd(Constance.Null2EmptyTrim(obj[0]));
						bean.setMchtCd(Constance.Null2EmptyTrim(obj[1]));
						bean.setTransCd(Constance.Null2EmptyTrim(obj[2]));
						bean.setBegin(Constance.Null2EmptyTrim(obj[3]));
						bean.setEnd(Constance.Null2EmptyTrim(obj[4]));
						bean.setFlag(Constance.Null2EmptyTrim(obj[5]));
						lists.add(bean);
					}
					searchRsList.clear();
					searchRsList.addAll(lists);
				}
			}
			
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
			
		} catch (Exception e) {
			
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
	   return SUCCESS;
    }
	
	/**
	 * 修改商户D0交易控制
	 * @return
	 */
	@Action(value = "changeSwitchInit", results = { @Result(name = "success", location="/pages/windControllSta/changeSwitch.jsp")})
	public String updateInit()
	{
		try {
			org();
			logger.info("D0商户交易控制页面初始化");
			bean=switchService.findMcthCdSwitch(bean);
			getRequest().setAttribute("flag", "update");
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("加载商户交易控制信息失败~,请重新修改");
		}
		return SUCCESS;
		
	}
	/**
	 * 添加商户D0交易控制
	 * @return
	 */
	@Action(value = "addSwitchinit", results = { @Result(name = "success", location="/pages/windControllSta/changeSwitch.jsp")})
	public String addSwitchInit()
	{
			try {
				org();
			} catch (Exception e) {
				logger.info("加载所属机构失败");
				e.printStackTrace();
			}
			logger.info("新增D0商户交易控制页面初始化");
		return SUCCESS;		
	}
	@Action(value="saveSwitch",results = { @Result(name = "success", location="/pages/windControllSta/switchList.jsp"),
			@Result(name = "failed", location="/pages/windControllSta/changeSwitch.jsp")})
	public String addSwitch()
	{
		try {
			Map map = switchService.addMcthSwitch(bean);
			org();
			if(map.containsKey("result"))
			{
				addActionMessage(String.valueOf(map.get("result")));
				return "failed";
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError("添加异常,请重新添加~");
		}
		addActionMessage("添加成功");
		return SUCCESS;
	}
	/**
	 * 更改D0商户交易控制
	 * @return
	 */
	@Action(value = "updateMchtSwitch", results = { @Result(name = "success", location="/pages/windControllSta/switchList.jsp"),
			@Result(name = "failed", location="/pages/windControllSta/changeSwitch.jsp")})
	public String updateStatus()
	{
		try {
			int count=switchService.updateMcthSwitch(bean);
			if(count==0)
			{
				addActionError("修改D0商户【"+bean.getMchtCd()+"】失败");
				return "failed";
			}
			addActionMessage("更改商户D0交易控制成功");
			org();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	@Action(value = "masterSwitchinit", results = { @Result(name = "success", location = "/pages/windControllSta/switchCore.jsp"),
			@Result(name = "error", location = "/error.jsp")
    })
	public String masterInit() throws Exception{
		Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(op.getRoleType().equals(Role.RISK_CONTROL_LEAD.getCode()) || op.getRoleType().equals(Role.RISK_CONTROL_OPERATORS.getCode())|| op.getRoleType().equals(Role.SYSTEM.getCode()))
		{
			logger.info("D0总交易控制页面初始化");
			bean=switchService.findMasterSwitch(null);
			if(bean==null)
			{
				addActionError("D0总交易控制页面初始化失败!!!");
				return ERROR;
			}
			return SUCCESS;
		}
		addActionError("无权限访问!!!");
		return ERROR;	   
    }
	
	/**
	 * 更改D0总交易控制
	 * @return
	 */
	@Action(value = "savemaster", results = { @Result(name = "success", location="/pages/windControllSta/switchList.jsp"),
			@Result(name = "failed", location="/pages/windControllSta/switchCore.jsp")})
	public String masterCore()
	{
		try {
			int count=switchService.updateMasterSwitch(bean);
			if(count==0)
			{
				addActionError("修改D0总交易控制失败");
				return "failed";
			}
			addActionMessage("更改D0总交易控制成功");
			org();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {

		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {

		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {

		getSession().setAttribute(SEARCH_SESSION_TRANS_SWITCH_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {

		return (BaseSearchCondition) getSession().getAttribute(SEARCH_SESSION_TRANS_SWITCH_QUERY);
	}

	@Override
	protected String getInitPageSize() {

		return Constance.PAGE_MAX_SIZE+"";
	}

	public SwitchSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(SwitchSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	public SwitchBean getBean() {
		return bean;
	}

	public void setBean(SwitchBean bean) {
		this.bean = bean;
	}
	public List<TblInsInf> getOrgList() {
		return orgList;
	}
	public void setOrgList(List<TblInsInf> orgList) {
		this.orgList = orgList;
	}
	
}
